package com.xxdd.kotlindemo

import android.content.Context
import android.content.res.AssetManager
import android.content.res.Configuration
import android.content.res.Resources
import dalvik.system.BaseDexClassLoader
import dalvik.system.DexClassLoader

lateinit var dexClassLoader: DexClassLoader
lateinit var resources: Resources


object PluginManagerImp {





    fun getResources() : Resources{

        return resources
    }

//    fun setContext(context: Context){
//        mContext = context
//    }

    fun getDexClassLoader() : DexClassLoader {
//        String dexPath, File optimizedDirectory, String librarySearchPath, ClassLoader parent

        return dexClassLoader
    }

    fun loadPath(context: Context, path : String){
        val dexOutFile = context.getDir("dex", Context.MODE_PRIVATE)
        dexClassLoader = DexClassLoader(path, dexOutFile?.absolutePath, null, context.classLoader);




        val assetManager = AssetManager::class.java.newInstance()
        val method = AssetManager::class.java.getMethod("addAssetPath", String::class.java)
        method.invoke(assetManager, path)

        val displayMetrics = context.resources.displayMetrics;
        val configuration = context.resources.configuration

        resources = Resources(assetManager, displayMetrics, configuration);
    }
}